package com.zbdx.controller;

import com.zbdx.domain.product;
import com.zbdx.service.SelProductService;
import com.zbdx.service.impl.SelProductServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @version 1.0
 * @Auther 樊航宇
 * @Date 2024/9/15 16:51
 * @注释
 */
@WebServlet("/shopping/productList")
public class SelProductServlet extends HttpServlet {

    private SelProductService selProductService = new SelProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //2.获取请求参数
        String method = req.getParameter("method");
        System.out.println("method = "+method);
        if("selectAllByType".equals(method)){
            try {
                this.selectAllBYType(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }else if("selectHeadByType".equals(method)){
            try {
                this.selectHeadByType(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    /**
     * 根据Type查询销量前10的各个类型的商品
     * @param req
     * @param resp
     */
    private void selectHeadByType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {


            List<product> productList01 = selProductService.findHeadProductbyType("女装男装");
            List<product> productList02 = selProductService.findHeadProductbyType("鞋类箱包");
            List<product> productList03 = selProductService.findHeadProductbyType("护肤彩妆");
            List<product> productList04 = selProductService.findHeadProductbyType("汇吃美食");
            List<product> productList05 = selProductService.findHeadProductbyType("手机数码");
            List<product> productList06 = selProductService.findHeadProductbyType("其他产品");

            req.setAttribute("productList01", productList01);
            req.setAttribute("productList02", productList02);
            req.setAttribute("productList03", productList03);
            req.setAttribute("productList04", productList04);
            req.setAttribute("productList05", productList05);
            req.setAttribute("productList06", productList06);
            System.out.println(productList01);
            System.out.println(productList02);
            System.out.println(productList03);
            System.out.println(productList04);
            System.out.println(productList05);
            System.out.println(productList06);

            // 转发请求到JSP页面
            RequestDispatcher dispatcher = req.getRequestDispatcher("shopping.jsp");
            dispatcher.forward(req, resp);
    }

    /**
     * 根据Type查询商品列表
     * @param req
     * @param resp
     */
    private void selectAllBYType(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {

        String ptype = req.getParameter("PType");
        System.out.println(ptype);

        if(ptype != null){

            List<product> productList = selProductService.findAllProductbyType(ptype);

            req.setAttribute("productList", productList);
            System.out.println(productList);

            // 转发请求到JSP页面
            RequestDispatcher dispatcher = req.getRequestDispatcher("productList.jsp");
            dispatcher.forward(req, resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
